package com.wzh.glsc.member.controller;

import com.wzh.common.exception.BizCodeEnume;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.R;
import com.wzh.common.vo.MemberVo;
import com.wzh.glsc.member.entity.MemberEntity;
import com.wzh.glsc.member.exception.PhoneExistException;
import com.wzh.glsc.member.exception.UserNameExistException;
import com.wzh.glsc.member.feign.CouponFeignService;
import com.wzh.glsc.member.service.MemberService;
import com.wzh.glsc.member.to.UserLoginTo;
import com.wzh.glsc.member.to.UserRegisterTo;
import com.wzh.glsc.member.to.WeiBoAccessToken;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;



/**
 * 会员
 *
 * @author lcm
 * @email 1291529969@gmail.com
 * @date 2022-03-18 19:26:43
 */
@RefreshScope
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    @Value("${member.user.name}")
    private String name;

    @RequestMapping("/test")
    public  R test(){
//        MemberEntity memberEntity=new MemberEntity();
//        memberEntity.setNickname("徐庶");
//        return R.ok().put("coupon",couponFeignService.test().get("coupon")).put("member", memberEntity);
        return  R.ok().put("name", name);
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/weibo/login")
    public R weiboLogin(@RequestBody WeiBoAccessToken weiBoAccessToken){
        MemberEntity memberEntity=memberService.weiboLogin(weiBoAccessToken);
        return R.ok().setData(memberEntity);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    @RequestMapping("/register")
    public R register(@RequestBody UserRegisterTo userRegisterTo){
        try {
            memberService.register(userRegisterTo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserNameExistException e){
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }catch (Exception e){
            return R.error("注册异常");
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    @RequestMapping("/login")
    @ResponseBody
    public R login(@RequestBody UserLoginTo userLoginTo) {
        MemberEntity memberEntity=memberService.login(userLoginTo);
        if(memberEntity!=null){
            Map<String,Object> map=new HashMap<>();
            MemberVo memberVo = new MemberVo();
            BeanUtils.copyProperties(memberEntity,memberVo);
            map.put("data", memberVo);
            return R.ok().setData(map);
        }
        return R.error(BizCodeEnume.LOGINACCT_PWD_EXCEPTION.getCode(), BizCodeEnume.LOGINACCT_PWD_EXCEPTION.getMsg());
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
